import 'package:boss/models/necessary_order.dart';
import 'package:boss/models/store_area.dart';
import 'package:boss/models/store_table.dart';
import 'package:boss/ui/widget/form/chip_form_field.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

List<ChipFormData<int>> billMethods = [
  ChipFormData(
    title: '按人数',
    value: 0,
  ),
  ChipFormData(
    title: '按桌台',
    value: 1,
  ),
];

List<ChipFormData<int>> useRanges = [
  ChipFormData(
    title: '全部',
    value: 0,
  ),
  ChipFormData(
    title: '扫码点餐',
    value: 1,
  ),
  ChipFormData(
    title: '收银机点餐',
    value: 3,
  ),
];

class NecessaryOrderDetailProvider extends ChangeNotifier {
  NecessaryOrder data;

  List<StoreTable> cacheTables = [];

  List<int> get selectMethod => [data.chargingMode];

  List<int> get selectUseRange => [data.useRange];

  String get tableText {
    if (data.useTableList.length == 0) return null;

    if (cacheTables.every((item) => data.useTableList.any((temp) => temp.tableId == item.tableId))) {
      return '全部';
    }

    var text = '';
    data.useTableList.forEach((table) {
      text += table.tableName;

      if (data.useTableList.last != table) {
        text += '，';
      }
    });

    return text;
  }

  NecessaryOrderDetailProvider({NecessaryOrder data}) {
    if (data != null) {
      this.data = data;
    } else {
      this.data = NecessaryOrder.fromJson({
        'chargingMode': 0,
        'useRange': 0,
      });
    }
  }

  setName(String value) {
    data.necessaryName = value;
    notifyListeners();
  }

  setPrice(String value) {
    try {
      data.price = AmountUtil.string2Price(value);
      notifyListeners();
    } catch (e) {
      data.price = null;
    }
  }

  setMethod(int value) {
    data.chargingMode = value;
    notifyListeners();
  }

  setCount(String value) {
    try {
      data.everyoneCount = int.parse(value);
      notifyListeners();
    } catch (e) {
      data.everyoneCount = null;
    }
  }

  setUseRange(int value) {
    data.useRange = value;
    notifyListeners();
  }

  setTables(List<StoreArea> areas) {
    cacheTables = [];

    for (var area in areas) {
      cacheTables.addAll(area.storeTableList);
    }
    print(data.necessaryId);

    if (data.necessaryId == null) {
      data.useTableList = cacheTables;
    }
    notifyListeners();
  }

  setSelectTable(List<StoreTable> tables) {
    data.useTableList = tables;
    notifyListeners();
  }
}
